package com.hncc.kumao.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtils {
    //创建连接池
    private static JedisPoolConfig config;
    private static JedisPool pool;

    static{
        try {
            PropertiesUtils propUtils = new PropertiesUtils("conf/kumao.properties");
            String ip = propUtils.get("redis.ip");
            int port = Integer.parseInt(propUtils.get("redis.port"));
            int maxTotal = Integer.parseInt(propUtils.get("redis.maxTotal"));
            int maxIdle = Integer.parseInt(propUtils.get("redis.maxIdle"));
            long minEvictableIdleTimeMillis = Long.parseLong(propUtils.get("redis.minEvictableIdleTimeMillis"));
            //System.out.println(ip + "\n" + port + "\n" + maxTotal + "\n" + maxIdle + "\n" + minEvictableIdleTimeMillis);
            config=new JedisPoolConfig();
            config.setMaxTotal(maxTotal);
            config.setMaxIdle(maxIdle);
            config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            pool=new JedisPool(config, ip, port);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //获取连接的方法
    public static Jedis getJedis(){
        return pool.getResource();
    }
    
    //释放连接
    public static void closeJedis(Jedis j){
        if(j != null) {
            j.close();            
        }
    }
}
